package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.jxc.dao.OverflowListDao;
import com.atguigu.jxc.dao.OverflowListGoodsDao;
import com.atguigu.jxc.entity.OverflowList;
import com.atguigu.jxc.entity.OverflowListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.OverflowListService;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class OverflowListServiceImpl implements OverflowListService {

    @Autowired
    private OverflowListDao overflowListDao;
    @Autowired
    private OverflowListGoodsDao overflowListGoodsDao;
    @Autowired
    private HttpSession session;

    @Transactional
    @Override
    public void save(OverflowList overflowList, String overflowListGoodsStr) {
        User user = (User) session.getAttribute("currentUser");
        overflowList.setUserId(user.getUserId());
        overflowListDao.save(overflowList);
        Integer overflowListId = overflowList.getOverflowListId();
        List<OverflowListGoods> overflowListGoods = JSONObject.parseArray(overflowListGoodsStr, OverflowListGoods.class);
        if (!CollectionUtils.isEmpty(overflowListGoods)){
            for (OverflowListGoods overflowListGood : overflowListGoods) {
                overflowListGood.setOverflowListId(overflowListId);
                overflowListGoodsDao.save(overflowListGood);
            }
        }
    }

    @Override
    public List<OverflowList> list(String sTime, String eTime) {
        List<OverflowList> overflowLists = overflowListDao.list(sTime,eTime);
        return overflowLists;
    }

    @Override
    public List<OverflowListGoods> goodsList(Integer overflowListId) {
        List<OverflowListGoods> overflowListGoods = overflowListGoodsDao.goodsList(overflowListId);
        return overflowListGoods;
    }
}
